#!/bin/bash
###
# @Author: admin@attacker.club
# @Date: 2022-07-30 18:03:20
# @LastEditTime: 2024-08-09 11:46:18
# @Description:
###
# curl  -s  http://wuyou.run/services/oraclejdk8.sh| bash

# 检查是否已安装
if [ -d /usr/local/jdk1.8* ]; then
    echo "已安装，退出安装"
    exit 0
fi

# 检查文件是否存在
if [ ! -f jdk-*.tar.gz ]; then
    # 下载
    wget -c https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
    # 检查下载是否成功
    if [ $?!= 0 ]; then
        echo "下载失败，退出安装"
        exit 1
    fi
fi

# 解压
tar -zxvf jdk-*.tar.gz -C /usr/local/

# 清理旧链接,创建新链接
rm -f /usr/bin/java
ln -s /usr/local/jdk1.8*/bin/java /usr/bin/java

# 编辑环境变量
if grep -q "JAVA_HOME" /etc/profile; then
    echo "JAVA_HOME 环境变量已存在，无需添加"
else
    echo "export JAVA_HOME=/usr/local/jdk1.8.0_202" >>/etc/profile
    echo "export CLASSPATH=\$JAVA_HOME/lib:\$CLASSPATH" >>/etc/profile
    echo "export JRE_HOME=\$JAVA_HOME/jre" >>/etc/profile
    echo "export PATH=\$JAVA_HOME/bin:\$PATH" >>/etc/profile
fi

# 使环境变量生效
. /etc/profile && java -version
